import React from 'react'
import { Button } from 'antd';
import { useState, useEffect } from 'react';
import { getClass } from '../../api/teacher.js'
import { useAuth } from "../../context/AuthProvider";
import { useHistory } from 'react-router-dom';

const Class = () => {
    const [classA, setClassa] = useState([])
    const { user } = useAuth()
    const history = useHistory();

    // const time = ["8:00-9:30", "9:45-11:15", "14:00-15:30", "15:45-17:15", "19:00-20:30"]
    function goScore(e, classname) {
        history.push({ pathname: '/home/tstu', state: { classname } })

    }
    const getClassAll = () => {
        console.log(user)
        getClass({ id:user.loginid,type:2 })
            .then(res => {
                console.log(res, "teacher", user);
                let m = 9, n = 7;
                let classArr = [];
                for (let i = 0; i < m; i++) {
                    let classArrin = []
                    for (let j = 0; j < n; j++) {
                        classArrin.push(0)
                    }
                    classArr.push(classArrin)
                }
                for (let i = 0; i < res.length; i++) {
                    classArr[res[i].time - 1][res[i].day - 1] = res[i]
                }
                console.log(classArr)
                setClassa(classArr)
                // setTotal(res.data.totalElements);
            })
    }
    useEffect(() => {
        getClassAll()
    }, [])
    return (
        <div>
            <table border="1" style={{ width: "100%", textAlign: "center" }}>
                {/* ,tableLayout:'fixed' */}
                <thead>
                    <tr>
                        <th>时间</th>
                        <th>星期一</th>
                        <th>星期二</th>
                        <th>星期三</th>
                        <th>星期四</th>
                        <th>星期五</th>
                        <th>星期六</th>
                        <th>星期日</th>

                    </tr>

                </thead>
                <tbody>
                    {
                        classA.map((item, index) => {
                            return <tr key={"class" + index}>
                                <td>
                                    <div>第{index + 1}节课

                                    </div></td>
                                {
                                    item.map((item2, index2) => {
                                        return <> <td key={"day" + index}>{item2 ? <div>
                                            课程：{item2.classname}<br />
                                            地址：{item2.address ? item2.address : "暂定"}<br />
                                            地址：{item2.address ? item2.address : "暂定"}<br />
                                        </div> : ""}</td></>
                                    })

                                }

                            </tr>
                        }
                        )
                    }
                </tbody>
            </table>
        </div>
    )
}

export default Class